package com.example.day03.controller;

import com.example.day03.pojo.Student;
import com.example.day03.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/findAll")
    public String findAll(Model model, @RequestParam(required = true, defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, 2);
        List<Student> list = studentService.findAll();
        PageInfo<Student> page = new PageInfo<>(list);
        model.addAttribute("list", page);
        return "list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "add";
    }

    @RequestMapping("/add")
    public String add(Student student){
        studentService.add(student);
        return "redirect:findAll";
    }

    @RequestMapping("/findById")
    public String findById(Model model,Integer id){
        Student student = studentService.findById(id);
        model.addAttribute("student", student);
        return "update";
    }

    @RequestMapping("/update")
    public String update(Student student){
        studentService.update(student);
        return "redirect:findAll";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        studentService.delete(id);
        return "redirect:findAll";
    }
}
